﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Xml;

namespace StoneShare.Harvest.AddIn
{
    public partial class RibbonHarvest
    {
        private void RibbonHarvest_Load(object sender, RibbonUIEventArgs e)
        {
           
            EnsureAppSettings();
            PopulateProjects();
           
        }

        private void PopulateProjects()
        {
            HarvestHelper hv = new HarvestHelper();
            XmlDocument doc = hv.GetMyProjects();
            XmlNodeList oProjects = doc.SelectNodes("/daily/projects/project");

            foreach (XmlNode oProject in oProjects)
            {
                //System.Diagnostics.Debug.Print(oProject.SelectSingleNode("name").InnerText);

                RibbonDropDownItem oProjectitem = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
                oProjectitem.Label = oProject.SelectSingleNode("name").InnerText;
                oProjectitem.Tag = oProject.SelectSingleNode("id").InnerText;
                ddProjects.Items.Add(oProjectitem);

                //XmlNodeList oTasks = oProject.SelectNodes("tasks/task");

                //foreach (XmlNode oTask in oTasks)
                //{
                //    //System.Diagnostics.Debug.Print(oTask.SelectSingleNode("task/name").InnerText);

                //    RibbonDropDownItem oTaskitem = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
                //    oTaskitem.Label = oTask.SelectSingleNode("name").InnerText;
                //    oTaskitem.Tag = oTask.SelectSingleNode("id").InnerText;
                //    ddProjects.Items.Add(oTaskitem);
                //}

            }

        }

        private void btnHarvest_Click(object sender, RibbonControlEventArgs e)
        {
            if (IsValidHours())
            {
                HarvestHelper hv = new HarvestHelper();
                string projectID = ddProjects.SelectedItem.Tag.ToString();
                hv.SubmitEntry(projectID, editBoxHours.Text);
                editBoxHours.Text = string.Empty;
            }
           

        }

        private bool IsValidHours()
        {
            if (editBoxHours.Text.Length == 0)
            {
                System.Windows.Forms.MessageBox.Show("Enter your Harvest hours for the selected project");
                return false;
            }
            double num;
            if (!System.Double.TryParse(editBoxHours.Text, out num))
            {
                System.Windows.Forms.MessageBox.Show("Enter a numeric value for your hours");
                return false;
            }

            return true;
        }

        private void EnsureAppSettings()
        {

            if((Properties.Settings.Default.userName.Length == 0) || 
                (Properties.Settings.Default.password .Length == 0) || 
                (Properties.Settings.Default.uri.Length == 0))
            {
                frmCredentials frm = new frmCredentials();
                frm.ShowDialog();
            }
        }

        private void btnSettings_Click(object sender, RibbonControlEventArgs e)
        {
            frmCredentials frm = new frmCredentials();
            frm.ShowDialog();
        }
    }
}
